package com.zh.work.controller.platform.channel;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.work.dao.platform.channel.QrCodeChannelRouteMapper;
import com.zh.work.entity.channel.QrCodeChannelRouteEntity;
import com.zh.work.entity.channel.QrCodeRouteMerchEntity;
import com.zh.work.service.platform.channel.QrCodeChannelRouteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 二维码路由规则管理
 * @Author: Li.HuiNeng
 * @Create in: 2019/09/04
 * @Version: 1.0.1
 */
@Api(value = "二维码路由规则管理控制器", produces = "application/json", tags = {"二维码路由规则管理控制器"})
@RestController
@RequestMapping("/qrCodeRoute")
public class QrCodeChannelRouteController {

    /**
     * @Description: 简易crud时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/04
     */
    @Autowired
    private QrCodeChannelRouteMapper mapper;

    /**
     * @Description: 有业务逻辑需要处理时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/04
     */
    @Autowired
    private QrCodeChannelRouteService service;


    @ApiOperation(value = "新增一条记录", tags = {"putAddQrCodeChannelRoute"}, notes = "新增一条记录")
    @PutMapping("/putAddQrCodeChannelRoute")
    @ResponseBody
    public JSONResult<Object> putAddQrCodeChannelRoute(@ApiParam(value = "二维码路由规则实体类", name = "routeEntity")
                                                    @RequestBody QrCodeChannelRouteEntity routeEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (service.putQrCodeChannelRoute(routeEntity) > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("添加失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "通过id修改信息", tags = {"putModQrCodeChannelRoute"}, notes = "通过id修改信息")
    @PutMapping("/putModQrCodeChannelRoute")
    @ResponseBody
    public JSONResult<Object> putModQrCodeChannelRoute(@ApiParam(value = "二维码路由规则实体类", name = "routeEntity")
                                                           @RequestBody QrCodeChannelRouteEntity routeEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = service.putModeChannelRqCodeRoute(routeEntity);
            if (i > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setSuccess(false);
                if (i == -1) {
                    jsonResult.setMessage("删除指定二维码商户信息失败!");
                } else if (i == -2)
                    jsonResult.setMessage("修改二维码信息失败!");
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键删除记录", tags = {"deleteQrCodeChannelRouteById"}, notes = "根据主键删除记录")
    @DeleteMapping("/deleteQrCodeChannelRouteById")
    public JSONResult<Object> deleteQrCodeChannelRouteById(@ApiParam(value = "二维码路由规则实体类", name = "routeEntity") QrCodeChannelRouteEntity routeEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (routeEntity.deleteById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键列表批量删除记录", tags = {"deleteQrCodeChannelRouteBatchByIds"}, notes = "该传参时 请选择raw(application/json)格式传入")
    @DeleteMapping("/deleteQrCodeChannelRouteBatchByIds")
    @ResponseBody
    public JSONResult<Object> deleteQrCodeChannelRouteBatchByIds(@ApiParam(value = "主键集合", name = "ids") @RequestParam("ids") @RequestBody List<Integer> ids) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int c = this.mapper.deleteBatchIds(ids);
            if (c > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据id查询详细信息", tags = {"getQrCodeChannelRouteById"}, notes = "根据id查询详细信息")
    @GetMapping("/getQrCodeChannelRouteById")
    public JSONResult<QrCodeChannelRouteEntity> getQrCodeChannelRouteEntityById(@ApiParam(value = "二维码路由规则实体类", name = "routeEntity")
                                                                                            QrCodeChannelRouteEntity routeEntity) {
        JSONResult<QrCodeChannelRouteEntity> jsonResult = new JSONResult<>();
        try {
            QrCodeChannelRouteEntity result = this.service.getById(routeEntity.getId());
            if (result != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(result);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesQrCodeChannelRoutes"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesQrCodeChannelRoutes")
    public JSONResult<List<QrCodeChannelRouteEntity>> getPagesQrCodeChannelRouteEntitys(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize") Integer pageSize,
            @ApiParam(value = "二维码路由规则实体类", name = "routeEntity") QrCodeChannelRouteEntity routeEntity) {
        JSONResult<List<QrCodeChannelRouteEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<QrCodeChannelRouteEntity> result = this.service.getChannelRoutePages(new Page<>(pageNo, pageSize), routeEntity);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改二维码路由规则状态", tags = {"putModQrCodeRouteStatus"}, notes = "修改二维码路由规则状态")
    @PutMapping("/putModQrCodeRouteStatus")
    @ResponseBody
    public JSONResult<Object> putModQrCodeRouteStatus(@ApiParam(value = "二维码路由实体类", name = "routeEntity")
                                                   @RequestBody QrCodeChannelRouteEntity routeEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = service.putChannelQrCodeStatus(routeEntity);
            if (i > 0) {
                jsonResult.setMessage("操作成功");
                jsonResult.setSuccess(true);
                jsonResult.setCode(200);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("网络繁忙，请稍后重试!");
            }
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "查询指定二维码路由商户", tags = {"getQrCodeMerchantByNo"}, notes = "查询指定二维码路由商户")
    @GetMapping("/getQrCodeMerchantByNo")
    public JSONResult<List<QrCodeRouteMerchEntity>> getQrCodeMerchantByNo(@ApiParam(value = "主键", name = "id")@RequestParam("id") Long id) {
        JSONResult<List<QrCodeRouteMerchEntity>> jsonResult = new JSONResult<>();
        try {
            List<QrCodeRouteMerchEntity> list = service.getQrCodeMerchantByNo(id);
            jsonResult.setData(list);
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

}
